package interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 项目名：PAIMSys
 * 包名：interceptor
 * 类名：LoginInterceptor
 * 描述：
 * 作者：柳胜酉
 * 开始时间：2023/4/26
 * 完成时间：
 * 版本：1.0
 */
public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String user_name = (String) session.getAttribute("user_name");
        // 如果用户已登陆也放行
        if (user_name != null) {
            System.out.println("已有用户登录：" + session.getAttribute("user_name"));
            return true;
        } else {
            System.out.println("没有用户登录！！！");
            // 用户没有登陆跳转到登陆页面
            request.getRequestDispatcher("/login").forward(request, response);
        }
        return false;
    }

}
